if (typeof(UI) === "undefined") {
    var UI = {};
}

// Class: UI.Segment

// Constructor: constructor
//
// Parameters:
//     data - Segment data.
//     layer - KineticJS layer to draw segment on.
UI.Segment = function (data, layer) {
    this.events = {
        // Event: clicked
        // Fired when this track is clicked.
        "clicked": new TypedEvent(),

        // Event: tapped
        // Fired when this track is tapped.
        "tapped": new TypedEvent(),

        // Event: entered
        // Fired when this track is entered.
        "entered": new TypedEvent(),

        // Event: exited
        // Fired when this track is exited.
        "exited": new TypedEvent()
    };

    this.layer = layer;

    this.rect = new Kinetic.Rect({
        "fill": "darkgrey",
        "stroke": "darkgrey",
        "strokeWidth": 1,
        "x": data.location.x,
        "y": data.location.y,
        "width": 8,
        "height": 29,
        "rotationDeg": data.location.rotation
    });
    this.layer.add(this.rect);

    this.rect.on("click", $.proxy(function () {
        this.events.clicked.fire();
    }, this));

    this.rect.on("tap", $.proxy(function () {
        this.events.clicked.fire();
    }, this));

    this.rect.on("mouseenter", $.proxy(function () {
        this.events.entered.fire();
    }, this));

    this.rect.on("mouseleave", $.proxy(function () {
        this.events.exited.fire();
    }, this));
};

// Method: highlight
// Highlights this segment with a border and optional color.
//
// Parameters:
//     color - Color to highlight segment with. Optional.
//             If undefined, the segment will remain its normal color.
UI.Segment.prototype.highlight = function (color) {
    this.rect.setStroke("black");
    if (color !== undefined && color !== null)
        this.rect.setFill(color);
    UI.Batch.drawLayer(this.layer);
};

// Method: lowlight
// Removes any highlighting on this segment.
UI.Segment.prototype.lowlight = function () {
    this.rect.setStroke("darkgrey");
    this.rect.setFill("darkgrey");
    UI.Batch.drawLayer(this.layer);
};

// Method: hide
// Hides this segment, removing it from the display.
UI.Segment.prototype.hide = function () {
    this.rect.setVisible(false);
    UI.Batch.drawLayer(this.layer);
};

// Method: show
// Restores this segment to the display.
UI.Segment.prototype.show = function () {
    this.rect.setVisible(true);
    UI.Batch.drawLayer(this.layer);
};

// Method: reset
// Resets the display of this segment.
UI.Segment.prototype.reset = function () {
    this.lowlight();
    this.show();
};
